package org.millioncall.yueyoga.admin.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.StringUtils;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.export.AbstractExcelExportTemplate;
import org.millioncall.yueyoga.admin.export.LiftclubExportTemplate;
import org.millioncall.yueyoga.admin.model.AviatorExpression;
import org.millioncall.yueyoga.admin.model.ClassMemberModel;
import org.millioncall.yueyoga.admin.model.Coach;
import org.millioncall.yueyoga.admin.model.Group;
import org.millioncall.yueyoga.admin.model.GroupUserModel;
import org.millioncall.yueyoga.admin.model.LiftClub;
import org.millioncall.yueyoga.admin.model.LiftClubAdvance;
import org.millioncall.yueyoga.admin.model.LiftClubRecord;
import org.millioncall.yueyoga.admin.model.Member;
import org.millioncall.yueyoga.admin.model.MemberClub;
import org.millioncall.yueyoga.admin.model.MemberLiftClub;
import org.millioncall.yueyoga.admin.model.Place;
import org.millioncall.yueyoga.admin.model.Status;
import org.millioncall.yueyoga.admin.model.User;
import org.millioncall.yueyoga.admin.model.UserDetail;
import org.millioncall.yueyoga.admin.service.CoachService;
import org.millioncall.yueyoga.admin.service.HallService;
import org.millioncall.yueyoga.admin.service.LiftClubService;
import org.millioncall.yueyoga.admin.service.MemberClubService;
import org.millioncall.yueyoga.admin.service.MemberService;
import org.millioncall.yueyoga.admin.service.PlaceService;
import org.millioncall.yueyoga.admin.service.StatusService;
import org.millioncall.yueyoga.admin.service.UserDetailService;
import org.millioncall.yueyoga.admin.service.UserService;
import org.millioncall.yueyoga.common.GlobalConstants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

@Controller
public class LiftClubAction extends BaseAction {
	private LiftClubService liftClubService;
	private PlaceService placeService;
	private HallService hallService;
	private CoachService coachService;
	private UserService userService;
	private UserDetailService userDetailService;
	private MemberService memberService;
	private MemberClubService memberClubService;
	private StatusService statusService;

	private static final String LIFTCLUB_SEARCH = "liftclub_search";
	private static final String ADDLIFTCLUB = "addLiftClub";
	private static final String LIFTCLUB_UPDATE = "liftclub_update";
	private static final String LIFTCLUB_DETAIL = "liftclub_detail";
	private static final String MEMBER_LIFTCLUB = "member_liftClub";
	@SuppressWarnings("unused")
	private static final String REDIRECT_SEARCH = "redirect:searchLiftClubs.jhtml";

	public MemberClubService getMemberClubService() {
		return memberClubService;
	}

	public void setMemberClubService(MemberClubService memberClubService) {
		this.memberClubService = memberClubService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserDetailService getUserDetailService() {
		return userDetailService;
	}

	public void setUserDetailService(UserDetailService userDetailService) {
		this.userDetailService = userDetailService;
	}

	public CoachService getCoachService() {
		return coachService;
	}

	public void setCoachService(CoachService coachService) {
		this.coachService = coachService;
	}

	public HallService getHallService() {
		return hallService;
	}

	public void setHallService(HallService hallService) {
		this.hallService = hallService;
	}

	public PlaceService getPlaceService() {
		return placeService;
	}

	public void setPlaceService(PlaceService placeService) {
		this.placeService = placeService;
	}

	public LiftClubService getLiftClubService() {
		return liftClubService;
	}

	public void setLiftClubService(LiftClubService liftClubService) {
		this.liftClubService = liftClubService;
	}
	
	/** 高级搜索活动 */
	@RequestMapping(value = "/ajaxAdvSearchLiftClub.jhtml")
	public void advSearchMember(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		String strLimit = request.getParameter("limit");

		String exp = request.getParameter("expression");
		String expType = request.getParameter("expType");

		String msg = "";

		int limit = Integer.MAX_VALUE;

		try {
			limit = Integer.parseInt(strLimit);
		} catch (NumberFormatException e) {
			limit = Integer.MAX_VALUE;
		}

		if (StringUtils.isBlank(exp)) {
			msg = "{'success':false, 'msg':'表达式不能为空！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}

		if (StringUtils.isBlank(expType)) {
			msg = "{'success':false, 'msg':'表达式类型不能为空！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}

		// 用于过滤会员的条件map
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("hallcode", this.getUser(request).getOrganizationId());

		// 条件表达式
		// 在此组装条件表达式
		AviatorExpression expression = new AviatorExpression();
		expression.setExpression(exp);
		// 条件表达式的map
		Map<String, Object> aviatorParams = new HashMap<String, Object>();
		aviatorParams.put(GlobalConstants.RESULT_ROW_MODEL_KEY, expType);

		List<LiftClubAdvance> liftClubs = this.liftClubService.getAllLiftClubsWithAviator(params, expression, aviatorParams, limit);

		if (liftClubs.size() > 0) {
			String json = JSON.toJSONStringWithDateFormat(liftClubs, "yyyy-MM-dd HH:mm", SerializerFeature.WriteDateUseDateFormat);
			msg = "{'success':true, 'total':'" + liftClubs.size() + "', 'items':"
					+ json + "}";
		} else {
			msg = "{'success':true, 'total':'0', 'items':''}";
		}

		WebAppUtils.ajaxResponse(response, msg);
	}

	/** 导出活动信息 **/
	@RequestMapping(value = "/exportliftclubInfo.jhtml", method = RequestMethod.POST)
	public void exportliftclubInfo(String name, String LiftClubState,
			String creditlimits, String coachType0Id, String coachType1Id,
			String responsibleId, String placename, String startdate1,
			String enddate1, String startdate2, String enddate2,
			ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {

		super.initFrontHeader(request, model);
		int start = 0;
		int limit = Integer.MAX_VALUE;
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");

		if (StringUtils.isNotBlank(pageSize)) {
			limit = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNotBlank(pageNo)) {
			start = (Integer.parseInt(pageNo) - 1) * limit;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(creditlimits)) {
			map.put("creditlimits", creditlimits);
		}

		if (StringUtils.isNotBlank(LiftClubState)) {
			map.put("LiftClubState", LiftClubState);
		}
		if (StringUtils.isNotBlank(name)) {
			map.put("name", name);
		}
		if (StringUtils.isNotBlank(responsibleId)) {
			map.put("responsibleId", responsibleId);
		}
		if (StringUtils.isNotBlank(coachType0Id)) {
			map.put("coachType0Id", coachType0Id);
		}
		if (StringUtils.isNotBlank(coachType1Id)) {
			map.put("coachType1Id", coachType1Id);
		}
		if (StringUtils.isNotBlank(startdate1)) {
			map.put("startdate1", startdate1);
		}
		if (StringUtils.isNotBlank(enddate1)) {
			map.put("enddate1", enddate1);
		}
		if (StringUtils.isNotBlank(startdate2)) {
			map.put("startdate2", startdate2);
		}
		if (StringUtils.isNotBlank(enddate2)) {
			map.put("enddate2", enddate2);
		}
		if (StringUtils.isNotBlank(placename)) {
			map.put("placename", placename);
		}
		
		List<Integer> groupIds = new ArrayList<Integer>();
		for (Group group : this.getUser(request).getGroups()) {
			groupIds.add(group.getId());
		}
		map.put("groups", groupIds);
		map.put("hallcode", this.getUser(request).getOrganizationId());
		PaginatedList<LiftClubRecord> pl = liftClubService.findLiftClubsByCdts(
				start, limit, map);
		LiftclubExportTemplate template = new LiftclubExportTemplate(pl);

		response.setContentType(AbstractExcelExportTemplate.EXCEL_TYPE);
		AbstractExcelExportTemplate.setFileDownloadHeader(request, response,
				"活动信息表.xlsx");
		try {
			template.doExport(response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/** 按条件查询 **/
	@RequestMapping(value = "/searchLiftClubs.jhtml")
	public String SearchId(String name, String LiftClubState,
			String creditlimits, String coachType0Id, String coachType1Id,
			String responsibleId, String placename, String startdate1,
			String enddate1, String startdate2, String enddate2,String display,
			ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		super.initFrontHeader(request, model);
		@SuppressWarnings("unused")
		User user = this.getUser(request);
		int start = 0;
		int limit = 10;
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		
		if("block".equals(display)){
			model.put("display",display);
		}
		
		if (StringUtils.isNotBlank(pageSize)) {
			limit = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNotBlank(pageNo)) {
			start = (Integer.parseInt(pageNo) - 1) * limit;
		}
		
		Map<String, Object> map = new HashMap<String, Object>();

		List<Integer> groupIds = new ArrayList<Integer>();
		for (Group group : this.getUser(request).getGroups()) {
			groupIds.add(group.getId());
		}
		map.put("groups", groupIds);
		map.put("hallcode", this.getUser(request).getOrganizationId());
		if (StringUtils.isNotBlank(creditlimits)) {
			map.put("creditlimits", creditlimits);
		}

		if (StringUtils.isNotBlank(LiftClubState)) {
			map.put("LiftClubState", LiftClubState);
		}
		if (StringUtils.isNotBlank(name)) {
			map.put("name", name);
		}
		if (StringUtils.isNotBlank(responsibleId)) {
			map.put("responsibleId", responsibleId);
		}
		if (StringUtils.isNotBlank(coachType0Id)) {
			map.put("coachType0Id", coachType0Id);
		}
		if (StringUtils.isNotBlank(coachType1Id)) {
			map.put("coachType1Id", coachType1Id);
		}
		if (StringUtils.isNotBlank(startdate1)) {
			map.put("startdate1", startdate1);
		}
		if (StringUtils.isNotBlank(enddate1)) {
			map.put("enddate1", enddate1);
		}
		if (StringUtils.isNotBlank(startdate2)) {
			map.put("startdate2", startdate2);
		}
		if (StringUtils.isNotBlank(enddate2)) {
			map.put("enddate2", enddate2);
		}
		if (StringUtils.isNotBlank(placename)) {
			map.put("placename", placename);
		}
		PaginatedList<LiftClub> p = null;
		int totalspeakers = 0;
		if("block".equals(display)){
		    p = liftClubService.getIdInPage(start, limit,
					map);
		    totalspeakers = p.getPager().getCountOfTotalResults().intValue();
		}
		// 获得场地
		Map<String, Object> mapPlace = new HashMap<String, Object>();
		mapPlace.put("groups", groupIds);
		mapPlace.put("hallcode",this.getUser(request).getOrganizationId());
		List<Place> plPlace = this.placeService.findAllByOrganizationId(mapPlace);
		
		// 查询教练
		Map<String, Object> mapT = new HashMap<String, Object>();
		mapT.put("groups", groupIds);
		mapT.put("hallcode", this.getUser(request).getOrganizationId());
		List<Coach> listCoach0 = this.coachService.findAllTeacherByOrganizationId(mapT);
		model.put("listCoach0", listCoach0);
		
		// 查询助教
		Map<String, Object> mapA = new HashMap<String, Object>();
		mapA.put("groups", groupIds);
		mapA.put("hallcode", this.getUser(request).getOrganizationId());
		List<Coach> listCoach1 = this.coachService.findAllAssistantByOrganizationId(mapA);
		model.put("listCoach1", listCoach1);
		// 查询负责人
		List<GroupUserModel> listUser = this.userService.findUsersByGroupId(
				"4", this.getUser(request).getOrganizationId());
		model.put("responsibles", listUser);

		model.put("LiftClubState", LiftClubState);
		model.put("responsibleId", responsibleId);
		model.put("liftClubs", p);
		model.put("totalspeakers", totalspeakers);
		model.put("plPlace", plPlace);
		model.put("clubPlaceId", placename);
		model.put("clubStartDate1", startdate1);
		model.put("clubEndDate1", enddate1);
		model.put("clubStartDate2", startdate2);
		model.put("clubEndDate2", enddate2);
		model.put("creditlimits", creditlimits);

		return LIFTCLUB_SEARCH;

	}

	/** 进入添加活动页 */
	@RequestMapping(value = "/toAddLiftClubs.jhtml")
	public String toAddLiftClubs(Map<String, Object> context,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) {
		super.initFrontHeader(request, map);
		
		List<Integer> groupIds = new ArrayList<Integer>();
		for (Group group : this.getUser(request).getGroups()) {
			groupIds.add(group.getId());
		}
		// 查询可用场地
		Map<String, Object> mapPlace = new HashMap<String, Object>();
		mapPlace.put("groups", groupIds);
		mapPlace.put("hallcode",this.getUser(request).getOrganizationId());
		List<Place> listPlace = this.placeService.findAllByOrganizationId(mapPlace);
		context.put("listPlace", listPlace);
		
		// 查询教练
		Map<String, Object> mapT = new HashMap<String, Object>();
		mapT.put("groups", groupIds);
		mapT.put("hallcode", this.getUser(request).getOrganizationId());
		List<Coach> listCoach0 = this.coachService.findAllTeacherByOrganizationId(mapT);
		context.put("listCoach0", listCoach0);
		
		// 查询助教
		Map<String, Object> mapA = new HashMap<String, Object>();
		mapA.put("groups", groupIds);
		mapA.put("hallcode", this.getUser(request).getOrganizationId());
		List<Coach> listCoach1 = this.coachService.findAllAssistantByOrganizationId(mapA);
		context.put("listCoach1", listCoach1);
		// 查询负责人
		List<GroupUserModel> listUser = this.userService.findUsersByGroupId(
				"4", this.getUser(request).getOrganizationId());
		context.put("responsibles", listUser);
		return ADDLIFTCLUB;
	}

	/** 添加活动 **/
	@RequestMapping(value = "/addLiftClubs.jhtml", method = RequestMethod.POST)
	public String addLiftClub(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		super.initFrontHeader(request, model);
		Map<String, Object> map = new HashMap<String, Object>();
		User user = this.getUser(request);
		map.put("organizationId", user.getOrganizationId());
		String liftClubName = request.getParameter("liftClubName");
		if (StringUtils.isNotBlank(liftClubName)) {
			map.put("liftClubName", liftClubName);
		}
		String LiftClubState = request.getParameter("LiftClubState");
		if (StringUtils.isNotBlank(LiftClubState)) {
			map.put("LiftClubState", LiftClubState);
		}
		String coach0 = request.getParameter("coachType0Id");
		if (StringUtils.isNotBlank(coach0)) {
			map.put("coach0", coach0);
		}
		String coach1 = request.getParameter("coachType1Id");
		if (StringUtils.isNotBlank(coach1)) {
			map.put("coach1", coach1);
		}
		String startdate = request.getParameter("startdate");
		if (StringUtils.isNotBlank(startdate)) {
			map.put("startdate", startdate);
		}
		String enddate = request.getParameter("enddate");
		if (StringUtils.isNotBlank(enddate)) {
			map.put("enddate", enddate);
		}
		String creditlimits = request.getParameter("creditlimits");
		if (StringUtils.isNotBlank(creditlimits)) {
			try {
				map.put("creditlimits", Integer.parseInt(creditlimits));
			} catch (Exception e) {
				map.put("creditlimits", 0);
			}
		}
		String responsibleId = request.getParameter("responsibleId");
		if (StringUtils.isNotBlank(responsibleId)) {
			map.put("responsibleId", responsibleId);
		}
		String placeId = request.getParameter("placeId");
		if (StringUtils.isNotBlank(placeId)) {
			map.put("placeId", placeId);
		}
		String remarks = request.getParameter("remarks");
		if (StringUtils.isNotBlank(remarks)) {
			map.put("remarks", remarks);
		}
		boolean b = liftClubService.addLiftClub(map);
		LiftClub lc = liftClubService.findLiftClubByCdts(map);
		Integer lcId = null;
		if(lc!=null){
			lcId = lc.getId();
		}
		if (b) {
			model.put("msg", "lcs");
			if(lcId!=null){
				return "redirect:toLiftClubDetail.jhtml?id="+lcId;
			}
		} else {
			model.put("msg", "lcf");
		}
		return ADDLIFTCLUB;
	}

	/** 删除活动 **/
	@RequestMapping(value = "/deleteLiftClub.jhtml")
	public void deleteLiftClub(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		// deleteByLiftClubId(id)
		String msg;
		String[] arrIds = request.getParameterValues("ids[]");

		List<String> strIds = Arrays.asList(arrIds);
		List<Integer> intIds = new ArrayList<Integer>();
		for (String strId : strIds) {
			intIds.add(Integer.valueOf(strId));
		}

		int i = this.liftClubService.deleteByLiftClubId(intIds);
		msg = i > 0 ? "success" : "failure";
		WebAppUtils.ajaxResponse(response, msg);
	}

	/** 跳转到修改活动页面 */
	@RequestMapping(value = "/toUpdateLiftClub.jhtml", method = RequestMethod.GET)
	public String toUpdateLiftClub(ModelMap context, HttpServletRequest request) {
		super.initFrontHeader(request, context);
		List<Integer> groupIds = new ArrayList<Integer>();
		for (Group group : this.getUser(request).getGroups()) {
			groupIds.add(group.getId());
		}
		
		// 查询教练
		Map<String, Object> mapT = new HashMap<String, Object>();
		mapT.put("groups", groupIds);
		mapT.put("hallcode", this.getUser(request).getOrganizationId());
		List<Coach> listCoach0 = this.coachService.findAllTeacherByOrganizationId(mapT);
		context.put("listCoach0", listCoach0);
		// 查询助教
		Map<String, Object> mapA = new HashMap<String, Object>();
		mapA.put("groups", groupIds);
		mapA.put("hallcode", this.getUser(request).getOrganizationId());
		List<Coach> listCoach1 = this.coachService.findAllAssistantByOrganizationId(mapA);
		context.put("listCoach1", listCoach1);
		// 查询可用场地
		Map<String, Object> mapPlace = new HashMap<String, Object>();
		mapPlace.put("groups", groupIds);
		mapPlace.put("hallcode",this.getUser(request).getOrganizationId());
		List<Place> listPlace = this.placeService.findAllByOrganizationId(mapPlace);
		context.put("listPlace", listPlace);
		// 查询活动
		String strId = request.getParameter("id");
		// 查询负责人
		List<GroupUserModel> listUser = this.userService.findUsersByGroupId(
				"4", this.getUser(request).getOrganizationId());
		context.put("responsibles", listUser);
		int id = 0;
		try {
			id = Integer.parseInt(strId);
		} catch (NumberFormatException e) {
			return LIFTCLUB_SEARCH;
		}

		LiftClub liftClub = this.liftClubService.selectById(id);
		context.put("liftClub", liftClub);
		int start = 0;
		int limit = 10;
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		if (StringUtils.isNotBlank(pageSize)) {
			limit = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNotBlank(pageNo)) {
			start = (Integer.parseInt(pageNo) - 1) * limit;
		}
		context.put("creditlimits", liftClub.getCreditlimits());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("groups", groupIds);
		map.put("hallcode", this.getUser(request).getOrganizationId());
		PaginatedList<Member> members = memberService.findMemberByLiftClubId(start, limit, map);
		context.put("members", members);

		return LIFTCLUB_UPDATE;
	}

	/** 修改活动 **/
	@RequestMapping(value = "/updateLiftClubs.jhtml", method = RequestMethod.POST)
	public String updateLiftClub(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		super.initFrontHeader(request, model);

		Map<String, Object> map = new HashMap<String, Object>();
		String strId = request.getParameter("liftClubId");
		if (StringUtils.isNotBlank(strId)) {
			map.put("id", strId);
			model.put("id", strId);
		}
		String liftClubName = request.getParameter("liftClubName");
		if (StringUtils.isNotBlank(liftClubName)) {
			map.put("liftClubName", liftClubName);
		}
		String LiftClubStatus = request.getParameter("LiftClubStatus");
		if (StringUtils.isNotBlank(LiftClubStatus)) {
			map.put("LiftClubStatus", LiftClubStatus);
		}
		String responsibleId = request.getParameter("responsibleId");
		if (StringUtils.isNotBlank(responsibleId)) {
			map.put("responsibleId", responsibleId);
		}
		String placename = request.getParameter("placename");
		if (StringUtils.isNotBlank(placename)) {
			map.put("placename", placename);
		}
		String coachType0Id = request.getParameter("coachType0Id");
		if (StringUtils.isNotBlank(coachType0Id)) {
			map.put("coachType0Id", coachType0Id);
		}
		String coachType1Id = request.getParameter("coachType1Id");
		if (StringUtils.isNotBlank(coachType1Id)) {
			map.put("coachType1Id", coachType1Id);
		}
		String startdate = request.getParameter("startdate");
		if (StringUtils.isNotBlank(startdate)) {
			map.put("startdate", startdate);
		}
		String enddate = request.getParameter("enddate");
		if (StringUtils.isNotBlank(enddate)) {
			map.put("enddate", enddate);
		}
		String creditlimits = request.getParameter("creditlimits");
		if (StringUtils.isNotBlank(creditlimits)) {
			try {
				map.put("creditlimits",Integer.parseInt(creditlimits));
			} catch (Exception e) {
				model.put("msg", "failure");
			}
		}
		String remarks = request.getParameter("remarks");
		if (StringUtils.isNotBlank(remarks)) {
			map.put("remarks", remarks);
		}
		boolean b = liftClubService.updateLiftClub(map);
		if (b) {
			model.put("msg", "success");
		} else {
			model.put("msg", "failure");
		}
		return LIFTCLUB_UPDATE;
	}

	/** 进入会员活动页,查询会员参加过的活动 **/
	@RequestMapping(value = "/toMemberLiftClub.jhtml")
	public String toMemberGoods(ModelMap context, Integer id,
			HttpServletRequest request, HttpServletResponse response) {

		super.initFrontHeader(request, context);

		int start = 0;
		int limit = 6;
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		if (StringUtils.isNotBlank(pageSize)) {
			limit = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNotBlank(pageNo)) {
			start = (Integer.parseInt(pageNo) - 1) * limit;
		}
		// 查询会员信息
		Map<String, Object> map1 = new HashMap<String, Object>();
		List<Integer> groupIds = new ArrayList<Integer>();
		for (Group group : this.getUser(request).getGroups()) {
			groupIds.add(group.getId());
		}
		map1.put("groups", groupIds);
		map1.put("hallcode", this.getUser(request).getOrganizationId());
		map1.put("id", Integer.valueOf(id));
		Member member = memberService.findById(map1);
		context.put("member", member);
		List<Status> status = statusService.findByType(1);// 根据类型查询父类状态
		context.put("memberStatus", status);
		if (member.getSubstatus() != null && !member.getSubstatus().equals("")) {
			Status stat = statusService.findByCode(member.getSubstatus());
			context.put("statu", stat);
			status = statusService.findByParentCode(stat.getParentcode());
			context.put("subStatuses", status);
			stat = statusService.findByCode(stat.getParentcode());
			context.put("parentStatu", stat);
		}
		// 查询所有活动,供会员选择参加哪个
		List<LiftClub> listLiftClubAll = liftClubService.findLiftClubAll();
		context.put("liftClubAll", listLiftClubAll);
		/** 查询会员参加过的活动 **/
		PaginatedList<LiftClub> listLiftClub = liftClubService
				.findLiftClubById(start, limit, id);
		context.put("memberClub", listLiftClub);
		return MEMBER_LIFTCLUB;
	}

	/** 添加会员选择加入的活动 **/
	@RequestMapping(value = "/tojoinLiftClub.jhtml")
	public String toAddLiftClub(ModelMap map, Integer clubId, Integer memberId,
			HttpServletRequest request, HttpServletResponse response) {
		super.initFrontHeader(request, map);
		Map<String, Object> model = new HashMap<String, Object>();
		if (clubId != null && memberId != null) {
			model.put("clubId", clubId);
			model.put("memberId", memberId);
			List<Integer> groupIds = new ArrayList<Integer>();
			for (Group group : this.getUser(request).getGroups()) {
				groupIds.add(group.getId());
			}
			model.put("groups", groupIds);
			List<MemberClub> memberClub = memberClubService.findMemberLiftClubById(model);
			if (memberClub.size() > 0) {
				map.put("memlifmsg", "join");
			} else {
				map.put("clubId", clubId);
				map.put("memberId", memberId);
				boolean b = memberClubService.saveMemberLiftClub(map);
				if (b) {
					map.put("memlifmsg", "success");
				} else {
					map.put("memlifmsg", "failure");
				}
			}
		}
		return MEMBER_LIFTCLUB;
	}

	/** 跳转到活动详情页面 */
	@RequestMapping(value = "/toLiftClubDetail.jhtml", method = RequestMethod.GET)
	public String toLiftClubDetail(ModelMap context, HttpServletRequest request) {
		super.initFrontHeader(request, context);
		String strId = request.getParameter("id");
		int id = 0;
		try {
			id = Integer.parseInt(strId);
		} catch (NumberFormatException e) {
			return LIFTCLUB_SEARCH;
		}

		LiftClub liftClub = this.liftClubService.selectById(id);
		context.put("id", liftClub.getId());
		context.put("name", liftClub.getName());
		context.put("status", liftClub.getStatus());
		String startdate = "";
		String enddate = "";
		if (StringUtils.isNotBlank(liftClub.getStartdate())) {
			startdate = liftClub.getStartdate().substring(0, 16);
		}
		context.put("startdate", startdate);
		if (StringUtils.isNotBlank(liftClub.getEnddate())) {
			enddate = liftClub.getEnddate().substring(0, 16);
		}
		context.put("enddate", enddate);
		context.put("placename", liftClub.getPlacename());
		context.put("username", liftClub.getUsername());
		UserDetail userDetail = userDetailService.findUserDetailById(liftClub
				.getResponsibleId());
		context.put("usernames", userDetail.getName());
		context.put("coach0name", liftClub.getCo0name());
		context.put("coach1name", liftClub.getCo1name());
		context.put("remarks", liftClub.getRemarks());
		if (liftClub.getStatus() == 0) {
			context.put("status", "准备中");
		} else if (liftClub.getStatus() == 1) {
			context.put("status", "进行中");
		} else if (liftClub.getStatus() == 2) {
			context.put("status", "活动结束");
		} else if (liftClub.getStatus() == 3) {
			context.put("status", "活动取消");
		}

		int start = 0;
		int limit = 10;
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		if (StringUtils.isNotBlank(pageSize)) {
			limit = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNotBlank(pageNo)) {
			start = (Integer.parseInt(pageNo) - 1) * limit;
		}
		context.put("creditlimits", liftClub.getCreditlimits());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<Integer> groupIds = new ArrayList<Integer>();
		for (Group group : this.getUser(request).getGroups()) {
			groupIds.add(group.getId());
		}
		map.put("groups", groupIds);
		map.put("hallcode", this.getUser(request).getOrganizationId());
		PaginatedList<Member> members = memberService.findMemberByLiftClubId(start, limit, map);

		context.put("members", members);
		
		Map<String, Object> map2 = new HashMap<String, Object>();
		List<Integer> groupIds2 = new ArrayList<Integer>();
		for (Group group : this.getUser(request).getGroups()) {
			groupIds2.add(group.getId());
		}
		map2.put("groups", groupIds2);
		map2.put("hallcode",this.getUser(request).getOrganizationId());
		List<ClassMemberModel> allMembers = this.memberService.findAllBasicMemberInfo(map2);
		context.put("allMembers", allMembers);

		return LIFTCLUB_DETAIL;
	}
	
	/** 添加会员到活动 */
	@RequestMapping(value="addMemberToLiftClub.jhtml")
	public void addMemberToLiftclub(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String strLiftclubId = request.getParameter("liftclubId");
		String strMemberId = request.getParameter("memberId");
		String msg = "";
		int liftclubId = 0;
		int memberId = 0;
		
		try {
			liftclubId = Integer.parseInt(strLiftclubId);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'活动编号格式不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		try {
			memberId = Integer.parseInt(strMemberId);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'会员编号格式不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		MemberLiftClub mem = new MemberLiftClub();
		mem.setMemberId(memberId);
		mem.setClubId(liftclubId);
		
		MemberLiftClub memExist = this.liftClubService.findLiftClubMember(mem);
		if(memExist != null) {
			msg = "{'success':false, 'msg':'会员已在该活动中！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		boolean b = this.liftClubService.addMemberToLiftClub(mem);
		
		if(b) {
			msg = "{'success':true, 'msg':'添加成功！'}";
		} else {
			msg = "{'success':false, 'msg':'添加失败！'}";
		}
		
		WebAppUtils.ajaxResponse(response, msg);
	}
	
	/** 从活动中删除 */
	@RequestMapping(value="deleteMemberFromLiftClub.jhtml")
	public void deleteMemberFromLiftClub(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String msg = "";
		String strLiftclubId = request.getParameter("liftclubId");
		String strMemberId = request.getParameter("memberId");
		int liftclubId = 0;
		int memberId = 0;
		
		try {
			liftclubId = Integer.parseInt(strLiftclubId);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'活动编号格式不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		try {
			memberId = Integer.parseInt(strMemberId);
		} catch (NumberFormatException e) {
			msg = "{'success':false, 'msg':'会员编号格式不正确！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		MemberLiftClub mem = new MemberLiftClub();
		mem.setMemberId(memberId);
		mem.setClubId(liftclubId);
		MemberLiftClub memExist = this.liftClubService.findLiftClubMember(mem);
		
		if(memExist == null) {
			msg = "{'success':false, 'msg':'会员已不在该活动中！'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		
		boolean b = this.liftClubService.deleteMemberFromLiftClub(memExist);
		if(b) {
			msg = "{'success':true, 'msg':'会员已从该活动中删除！'}";
		} else {
			msg = "{'success':false, 'msg':'从该活动中删除会员失败！'}";
		}
		
		WebAppUtils.ajaxResponse(response, msg);
	}

	public StatusService getStatusService() {
		return statusService;
	}

	public void setStatusService(StatusService statusService) {
		this.statusService = statusService;
	}
}
